﻿#region Includes
using System;
using System.Collections.Generic;

using OpenTK;
using OpenTK.Graphics.OpenGL;

using GingerGL;
using GingerGL.Math;
#endregion

namespace GingerGL.Graphics
{
    //------------------------------------------------------------//
    /// <summary>
    /// Wire Fram Grid Map with optional texture load
    /// </summary>
    public class GridMap : Mesh
    {
        //--------------------------------------------------------//
        #region Variables
        /// <summary>
        /// Gets the size of the Grid Map
        /// </summary>
        public float Size { get; private set; }
        #endregion
        //--------------------------------------------------------//
        #region Constructors
        /// <summary>
        /// initalizes a new Grid Map
        /// </summary>
        /// <param name="size">Size of Map</param>
        public GridMap(float size) 
            : base(BeginMode.Triangles, VertexUsage.Position) 
        { 
            this.Size = size; 
            BuildMe(); 
            this.Visible = true; 
        }
        /// <summary>
        /// initalizes a new Grid Map
        /// </summary>
        /// <param name="size">Size of Map</param>
        /// <param name="texture">Texture to Repeat</param>
        public GridMap(float size, string texture) 
            : base(BeginMode.Triangles,VertexUsage.Position | VertexUsage.Texture0)
        {
            Size = size;
            Materials.Add(new Material(System.Drawing.Bitmap.FromFile(texture), true));
            BuildMe();
            this.Visible = true;
        }
        #endregion
        //--------------------------------------------------------//
        #region Methods
        /// <summary>
        /// Build My Vertices
        /// </summary>
        private void BuildMe()
        {
            Buffer.Invalidate();

            float width = Size * 1.0f;
            Buffer.IndexBuffer.Write(new int[] { 0, 1, 2, 3, 4, 5 });
            Buffer.VertexBuffer.Write(new Vertex(new Vector3(-width, 0, -width), new Vector2(0, 0)));
            Buffer.VertexBuffer.Write(new Vertex(new Vector3(-width, 0, width), new Vector2(0, 1)));
            Buffer.VertexBuffer.Write(new Vertex(new Vector3(width, 0, width), new Vector2(1, 1)));
            Buffer.VertexBuffer.Write(new Vertex(new Vector3(-width, 0, -width), new Vector2(1, 1)));
            Buffer.VertexBuffer.Write(new Vertex(new Vector3(width, 0, width), new Vector2(0, 1)));
            Buffer.VertexBuffer.Write(new Vertex(new Vector3(width, 0, -width), new Vector2(1, 0)));
            
            Buffer.Commit();
        }
        /// <summary>
        /// Draws Map
        /// </summary>
        public override void Draw()
        {
            if (VertexUsage.HasFlag(VertexUsage.Texture0))
            {
                base.Materials[Mesh.Texture0].Apply();
                base.Draw();
            }

            for (float i = -Size; i <= Size; i += 5)
            {
                GL.Begin(BeginMode.Lines);
                GL.Color3(150, 190, 150);
                GL.Vertex3(-Size, 0, i);
                GL.Vertex3(Size, 0, i);
                GL.Vertex3(i, 0, -Size);
                GL.Vertex3(i, 0, Size);
                GL.End();
            }
        }
        /// <summary>
        /// Creates a new grid map object
        /// </summary>
        static public GridMap Create(float size) { return new GridMap(size); }
        /// <summary>
        /// Creates a new grid map object
        /// </summary>
        static public GridMap Create(float size, string image) { return new GridMap(size, image); }
        #endregion
        //--------------------------------------------------------//
    }
    //------------------------------------------------------------//
}
